<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8"/>
    <title>商品编辑</title>
    <script type="text/javascript" src="/static/js/form-check.js"></script>
    <script type="text/javascript">
        let addForm, addInputs, addInputNumber, addInputDecimalNumber;
        window.onload = function () {
            initViews();
            bindListeners();
        };

        function initViews() {
            addForm = document.getElementById("addForm");
            addInputs = document.getElementsByClassName("addInput");
            addInputNumber = document.getElementsByClassName("addInputNumber");
            addInputDecimalNumber = document.getElementsByClassName("addInputDecimalNumber");
        }

        function bindListeners() {
            addForm.onsubmit = checkSubmit;
        }

        function checkSubmit() {
            const arr = [];
            for (let i = 0; i < addInputs.length; i++) {
                const item = addInputs[i];
                if (!checkNotNull(item, null)) {
                    arr.push(1);
                    item.style.backgroundColor = "#ff654d";
                } else {
                    item.style.backgroundColor = "#fff";
                }
            }
            for (let i = 0; i < addInputNumber.length; i++) {
                const item = addInputNumber[i];
                if (!checkNumberMtZero(item, null)) {
                    arr.push(1);
                    item.style.backgroundColor = "#ff654d";
                } else {
                    item.style.backgroundColor = "#fff";
                }
            }
            for (let i = 0; i < addInputDecimalNumber.length; i++) {
                const item = addInputDecimalNumber[i];
                if (!checkDecimalMtZero(item, null)) {
                    arr.push(1);
                    item.style.backgroundColor = "#ff654d";
                } else {
                    item.style.backgroundColor = "#fff";
                }
            }
            if (arr.length === 0) {
                var channelIds = document.getElementsByName("channelIds");
                var aa = [];
                for (let i = 0; i < channelIds.length; i++) {
                    aa.push(1);
                }
                if (aa.length === 0) {
                    alert("请至少选择一个销售渠道");
                    return false;
                } else {
                    return true;
                }
            } else {
                alert("请确保信息的完整性和正确性！");
                return false;
            }
        }

    </script>
</head>
<body>
<form action="/comm/addComm.do" method="post" id="addForm">
    <table cellspacing="0" cellpadding="5" border="0">
        <tr>
            <td>商品名称</td>
            <td><input type="text" name="commName" class="addInput"/></td>
        </tr>

        <tr>
            <td>品牌</td>
            <td><input type="text" name="commBrandStr" class="addInput"></td>
        </tr>

        <tr>
            <td>商品类型</td>
            <td><input type="text" name="commTypeStr" class="addInput"/></td>
        </tr>

        <tr>
            <td>商品编码</td>
            <td><input type="text" name="commCode" class="addInputNumber"/></td>
        </tr>

        <tr>
            <td>商品状态</td>
            <td>
                <select name="state" id="mySelect">
                    <option value="0">未上架</option>
                    <option value="1">上架</option>
                    <option value="2">促销</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>商品条形码</td>
            <td><input type="text" name="commBarCode" class="addInputNumber"/></td>
        </tr>

        <tr>
            <td>进货价</td>
            <td><input type="text" name="jhPrice" class="addInputDecimalNumber"/></td>
        </tr>

        <tr>
            <td>库存</td>
            <td><input type="text" name="stock" class="addInputNumber"/></td>
        </tr>

        <tr>
            <td>销售渠道</td>
            <td>
                <table cellpadding="2" cellspacing="0" border="1" width="200px">
                    <tr aria-colspan="4" style="text-align: right">
                        <input type="button" value="新增渠道"
                               onclick="location.href='/comm/goToAddChannel?successPage=/comm/goToAddComm'"/>
                    </tr>
                    <tr>
                        <th>渠道</th>
                        <th>选择</th>
                    </tr>
                    <#list channels as data>
                        <tr>
                            <td>${data.channelName}</td>
                            <td>
                                <input type="checkbox" value="${data.id}" name="channelIds"/>
                            </td>
                        </tr>
                    </#list>
                </table>
            </td>
        </tr>

        <tr>
            <td>商品信息</td>
            <td>
                <table cellspacing="0" cellpadding="3" border="0">
                    <tr>
                        <td>容量：</td>
                        <td><input type="text" name="capacity" class="addInputDecimalNumber"></td>
                        <td>重量：</td>
                        <td><input type="text" name="weight" class="addInputDecimalNumber"></td>
                    </tr>
                    <tr>
                        <td>产地：</td>
                        <td><input type="text" name="origin" class="addInput"/></td>
                        <td>包装单位：</td>
                        <td><input type="text" name="packingUnit" class="addInput"/></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <input type="submit" value="新增"/>
                &nbsp;&nbsp;&nbsp;
                <input type="button" value="返回" onclick="window.history.back()"/>
                &nbsp;&nbsp;&nbsp;
            </td>
        </tr>
    </table>

</form>
</body>
</html>